//
//  RXBAlertView.m
//  RXBAlertView-master
//
//  Created by Mac on 2018/12/6.
//  Copyright © 2018年 johnren. All rights reserved.
//

#import "RXBAlertView.h"
#define ContentWidth [UIScreen mainScreen].bounds.size.width - (20 + 20 + 15 + 15)
#define TagValue  1000
#define AlertTime 0.3 //弹出动画时间
#define DropTime 0.5 //落下动画时间
@interface RXBAlertView()
/**标题*/
@property(nonatomic,strong)UILabel * title;
/**内容*/
@property(nonatomic,strong)UILabel * content;
/**确定按钮*/
@property(nonatomic,strong)UIButton * sureButton;
/**取消按钮*/
@property(nonatomic,strong)UIButton * cancelButton;
@end
@implementation RXBAlertView
-(UILabel *)title{
    if(!_title){
        _title = [[UILabel alloc]init];
        [_title setFont:[UIFont boldSystemFontOfSize:16]];
        [_title setTextColor:[UIColor blackColor]];
        _title.numberOfLines = 1;
        [_title sizeToFit];
    }
    return _title;
}
-(UILabel *)content{
    if(!_content){
        _content = [[UILabel alloc]init];
        [_content setFont:[UIFont systemFontOfSize:16]];
        _content.numberOfLines = 0;
        _content.textAlignment = NSTextAlignmentLeft;
        [_content setTextColor:[UIColor blackColor]];
    }
    return _content;
}
-(UIButton *)sureButton{
    if(!_sureButton){
        _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureButton addTarget:self action:@selector(clickSureButton:) forControlEvents:UIControlEventTouchUpInside];
        [_sureButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        _sureButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _sureButton;
}
-(UIButton *)cancelButton{
    if(!_cancelButton){
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton addTarget:self action:@selector(clickCancelButton:) forControlEvents:UIControlEventTouchUpInside];
        [_cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        _cancelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _cancelButton;
}


-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)title content:(NSString *)content mode:(VIEWMODE)mode sureButtonTitle:(NSString *)sureButtonTitle cancelButtonTitle:(NSString *)cancelButtonTitle sureButtonBlock:(void (^)(void))sureButtonBlock cancelButtonBlock:(void (^)(void))cancelBlock{
    if(self = [super initWithFrame:frame]){
    [self.title setText:title];
    [self.content setText:content];
    [self.sureButton setTitle:sureButtonTitle forState:UIControlStateNormal];
    [self.cancelButton setTitle:cancelButtonTitle forState:UIControlStateNormal];
    [self addSubview:self.title];
    [self addSubview:self.content];
    [self addSubview:self.sureButton];
    [self addSubview:self.cancelButton];
    /**进行界面设置*/
    self.backgroundColor = [UIColor whiteColor];
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 5;
    
    self.title.center = CGPointMake(self.center.x, 15);
        
        self.content.frame = CGRectMake(15, 30, frame.size.width - 30, frame.size.height - 80);
        
        self.sureButton.frame = CGRectMake(0, frame.size.height-30, frame.size.width * 0.5, 30);
        self.cancelButton.frame = CGRectMake(frame.size.width * 0.5, frame.size.height - 30, frame.size.width * 0.5, 30);
        
    }
    return self;
}
-(void)clickSureButton:(UIButton *)button{
    /**首先要进行隐藏*/
    [self hide];
    if(self.sureBlock){
        self.sureBlock();
    }
}
-(void)clickCancelButton:(UIButton *)button{
    [self hide];
    if(self.cancelBlock){
        self.cancelBlock();
    }
}

-(void)hide{
    if (self.superview) {
        [UIView animateWithDuration:AlertTime animations:^{
            self.transform = CGAffineTransformScale(self.transform,0.1,0.1);
            self.alpha = 0;
        } completion:^(BOOL finished) {
            UIView *bgview = [[UIApplication sharedApplication].keyWindow viewWithTag:TagValue];
            if (bgview) {
                [bgview removeFromSuperview];
            }
            [self removeFromSuperview];
        }];
    }
}
-(void)show{
    if (self.superview) {
        [self removeFromSuperview];
    }
    UIView *oldView = [[UIApplication sharedApplication].keyWindow viewWithTag:TagValue];
    if (oldView) {
        [oldView removeFromSuperview];
    }
    UIView *view = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    view.tag = TagValue;
    view.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [view addGestureRecognizer:tap];
    view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.center = [UIApplication sharedApplication].keyWindow.center;
    self.alpha = 0;
    self.transform = CGAffineTransformScale(self.transform,0.1,0.1);
    [UIView animateWithDuration:.2 animations:^{
        self.transform = CGAffineTransformIdentity;
        self.alpha = 1;
    }];
}


@end
